

#include "Door.h"
#include "BSP_Door.h"
#include "Timer.h"
#include "Env.h"

static Timer_T doorTimer;

void Door_Lock(void){	
	BSP_Door_Control(RELAY_S_CLOSE);
}

void Door_DoUnlock(void){
	BSP_Door_Control(RELAY_S_OPEN);
}

static void DoorCallback(void){
	Door_Lock();
}

void Door_Init(void){
	Door_Lock();
	Timer_Setup(&doorTimer, 500, 0, 0, DoorCallback);
}


uint8_t Door_Mode(void){
	return envProp.doorMode;
}

void Door_SetMode(uint8_t mode){
	Env_Set(&envProp.doorMode, &mode, sizeof(mode));
//		Door_Lock();
}

uint8_t Door_State(void){
	uint8_t state =  BSP_Door_State();
	if(state == RELAY_S_CLOSE){		
		return DOOR_S_LOCK;
	}
	else{		
		return DOOR_S_UNLOCK;
	}
}

void Door_Unlock(uint8_t seconds)
{
	Door_DoUnlock();
	if(seconds == 0){
		return;
	}
	seconds = seconds > 60 ? 60 : seconds;
	Timer_Reset(&doorTimer, seconds * 100);
}



